package lol.clann.minecraft.springboot.plugin.bukkit.command.resolver.factory;

import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolver;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolverFactory;
import org.bukkit.command.CommandException;
import org.bukkit.util.Vector;
import org.springframework.stereotype.Component;

/**
 *
 * @author pyz
 * @date 2019/5/6 9:29 PM
 */
@Component
public class VectorCommandResolverFactory implements CommandResolverFactory {
    @Override
    public boolean canResolve(Class<?> type) {
        return Vector.class.isAssignableFrom(type);
    }

    @Override
    public CommandResolver getResolver(Class<?> clazz) {
        return (sender, arg, type) -> {
            String[] parts = arg.split(",");
            if (parts.length != 3) {
                throw new CommandException("格式错误:" + arg + ",正确格式应该为: x,y,z");
            }
            double x;
            double y;
            double z;
            try {
                x = Double.valueOf(parts[0]);
                y = Double.valueOf(parts[1]);
                z = Double.valueOf(parts[2]);
            } catch (NumberFormatException e) {
                throw new CommandException("请输入正确的数字:" + arg);
            }
            return new Vector(x, y, z);
        };
    }
}
